<template>
  <div class="container">
    <div class="app-container">
      <el-tree default-expand-all :data="depts" :props="defaultProps" :expand-on-click-node="false">
        <!-- 节点结构 -->
        <!-- v-slot="{name,data}" 只能作用于template -->
        <template v-slot="{data}">
          <el-row style="width:100%;height:40px" type="flex" justify="space-between" align="middle">
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="tree-manager">{{ data.managerName }}</span>
              <!-- $event 实参 表示类型 -->
              <el-dropdown @command="operateDept($event,data.id)">
                <!-- 显示区域内容 -->
                <span>
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <!-- 下拉菜单选项 -->
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add" icon="el-icon-plus">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit" icon="el-icon-edit">编辑子部门</el-dropdown-item>
                  <el-dropdown-item command="del" icon="el-icon-delete">删除子部门</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <AddDept ref="addDept" v-dialogDrag :show-dialog.sync="showDialog" :current-node-id="currentNodeId" @updateDepartment="getDepartment" />
  </div>
</template>
<script>
// 导入获取组织架构的接口
import { deleteDepartment, getDepartment } from '@/api/department'
// 导入将列表数据转为树结构的方法
import { transListToTreeData } from '@/utils'
import AddDept from './components/add-dep.vue'

export default {
  name: 'Department',
  components: {
    AddDept
  },
  data() {
    return {
      depts: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false,
      currentNodeId: null // 存储当前节点id
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    // 获取组织架构树数据
    async getDepartment() {
      const res = await getDepartment()
      // console.log(res)
      this.depts = transListToTreeData(res, 0)
    },
    // 添加修改删除注册事件
    operateDept(type, id) {
      // console.log(type, id)
      if (type === 'add') {
        this.showDialog = true
        this.currentNodeId = id
      } else if (type === 'edit') {
        this.showDialog = true
        this.currentNodeId = id
        // 这里为什么要用$nextTick，因为弹出层之后紧接着要调用子组件方法，第一次弹出层时，里面的组件还未完成渲染，
        // 所以要等到showDialog这个属性设置的数据完成渲染之后才进行调用
        this.$nextTick(() => {
          this.$refs.addDept.getDepartmentDetail()
        })
      } else {
        // 删除部门
        this.$confirm('您确定要删除部门吗？').then(async() => {
          await deleteDepartment(id)
          this.$message.success('删除部门成功')
          this.getDepartment()
        })
      }
    }
  }
}
</script>
<style scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}
.tree-manager {
  width: 100px;
  display: inline-block;
  margin: 10px;
}
</style>
